package com.zyc.demo.v.config;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter;
import org.springframework.web.accept.HeaderContentNegotiationStrategy;
import org.springframework.web.accept.ParameterContentNegotiationStrategy;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@SpringBootConfiguration
public class WebMvcConfig implements WebMvcConfigurer {
    /**
     * 允许跨域配置
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //允许客户端携带自定义请求头
                .allowCredentials(false)
                //域名 e.g."https://domain.com" 生产环境不允许设置为“*”
                .allowedOrigins("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "TRACE")
                .maxAge(3600)
                .allowedHeaders("*");
    }

    // 配置内容协商选项
    // 使用这个功能需要在application.properties中开启 spring.mvc.contentnegotiation.favor-parameter=true
    // 默认情况下只要参数携带 format=json 就会返回对应的格式数据
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        // 请求：http://localhost/index?format=x-hello-test-111
        // 按照最佳匹配规则会匹配到 application/x-hello-test 类型的消息转换器进行转换
        // configurer.mediaType("x-hello-test-111", MediaType.parseMediaType("application/x-hello-test;charset=UTF-8"));
        // configurer.mediaType("xml",MediaType.APPLICATION_XML);

        HashMap<String, MediaType> map = new HashMap<>();
        map.put("json", MediaType.APPLICATION_JSON);
        map.put("xml", MediaType.APPLICATION_XML);// 内容协商加入xml的方式解析
        /*map.put("cbor", MediaType.APPLICATION_CBOR);
        map.put("x-jackson-smile", new MediaType("application", "x-jackson-smile"));
        // 可以自定义媒体类型，但是需要自己去实现一个 HttpMessageConverter 消息转换器
        map.put("test", MediaType.parseMediaType("application/x-hello-test"));*/

        // 根据参数来确定媒体类型类策略
        ParameterContentNegotiationStrategy parameterNegotiationStrategy = new ParameterContentNegotiationStrategy(map);
        configurer.strategies(Collections.singletonList(parameterNegotiationStrategy));
        /*// 根据请求头来协商媒体类型策略
        HeaderContentNegotiationStrategy headerContentNegotiationStrategy = new HeaderContentNegotiationStrategy();
        configurer.strategies(Arrays.asList(parameterNegotiationStrategy, headerContentNegotiationStrategy));*/
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {

    }
}